package com.yyfax.infrastructure.upgrade;

import android.content.Context;

import com.yyfax.common.CLog;
import com.yyfax.common.tools.ManifestUtil;
import com.yyfax.infrastructure.base.BaseApplication;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by HenryZhang on 2016/11/23.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class CheckVersionInfoTask extends CompareLocalRemoteTask {
	private static final String TAG = "CheckVersionInfoTask";

	private static final String CHANNEL = "UMENG_CHANNEL";
	private static final String APK_NAME = "yylending-";

	public static final int MODE_CUSTOM = 0;
	public static final int MODE_DEFAULT = 1;

	private int mMode = MODE_DEFAULT;

	private String mUrl;
	private String mParams;
	private String mVersionConfigURL = mUrl + "ver_config.json";
	private String mInternalConfigURL = mUrl + "ver_config_internal.json";

	public CheckVersionInfoTask(Context ctx, String url, String params, int mode) {
		super(ctx);
		mUrl = url;
		mParams = params;
		mMode = mode;
	}

	public static class VersionInfo {
		public String msg;
		public int configVersion = 1;
		public boolean hasNewVersion = false;
		public boolean forceUpdate = false;
		public int versionCode;
		public String versionName;
		public String versionDesc;
		public String apkUrl;
		public String apkName;
		public String applicationId;
	}

	protected String getDownloadUrl() {
		if (MODE_CUSTOM == mMode)
			return mUrl;
		Context ctx = BaseApplication.app();
		final String channel = ManifestUtil.readMetaDataFromApplication(ctx, CHANNEL);
		final String configUrl = channel.equals("internal") ? mInternalConfigURL : mVersionConfigURL;
		CLog.d(TAG, "channel: " + channel + ", config_file_url: " + configUrl);
		return configUrl;
	}

	@Override
	protected String getParams() {
		return mParams;
	}

	@Override
	protected Object parseContent(String content) {
		if (content == null)
			return null;

		String channel = "internal";
		Context ctx = mContext.get();
		if (ctx != null) {
			channel = ManifestUtil.readMetaDataFromApplication(ctx, CHANNEL);
		}

		try {
			JSONObject result = new JSONObject(content);
			String status = result.optString("", "success");
			if (!"success".equals(status)) {
				String msg = result.optString("msg");//todo 手动检查时弹提示
				VersionInfo info = new VersionInfo();
				info.msg = msg;
				return info;
			} else {
				JSONObject json = result.optJSONObject("content");
				if (json != null) {
					int configVersion = json.optInt("configVersion", 1);
					int versionCode = json.optInt("versionCode");//todo 格式16120301(年月日+当日编译号)
					String versionName = json.optString("versionName");
					String versionDesc = json.optString("versionDesc");
					String downloadUrl = json.optString("downloadUrl");
					boolean forceUpdate = json.optInt("forceUpdate", 1) == 1;
					boolean enableAll = json.optInt("enableAll", 1) == 1;
					String channels = json.optString("channels");

					boolean contain = channels.contains(channel);
					boolean enable = enableAll || contain;
					CLog.i(TAG, "channels can update = " + contain);
					if (!enable) {
						CLog.i(TAG, "Don't check new version for me.");
					} else {
						int currentVersionCode = 1;
						Context c = mContext.get();
						if (c != null) {
							currentVersionCode = Assistant.getVersionCode(c);
						}
						if (versionCode > currentVersionCode) {
							final String apkName = APK_NAME + channel + ".apk";//todo 考虑添加版本号
							final String realApkUrl = downloadUrl.replace("xxx", channel);//todo 服务端接口也要写成可替换，且要有对应渠道号的APK文件

							VersionInfo info = new VersionInfo();
							info.configVersion = configVersion;
							info.hasNewVersion = true;
							info.forceUpdate = forceUpdate;
							info.versionCode = versionCode;
							info.versionName = versionName;
							info.versionDesc = versionDesc;
							info.apkUrl = realApkUrl;
							info.apkName = apkName;
							return info;
						}
					}
				}
			}
		} catch (JSONException e) {
			return null;
		}
		return null;
	}
}
